home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewsSplash-c / NewsSplash.µ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  6.9 KB  |  302 lines  |  [TEXT/MMCC]

  1. //• --------------------------------------------------------------- •//
  2. //• A public domain demo of a "news flash" text splash screen,        •//
  3. //• courtesy of Kenneth A. Long (kenlong@netcom.com).                •//
  4. //• An itty bitty bytes™ production, for the benefit of anyone        •//
  5. //• who wants use it.                                                •//
  6.  
  7. //• This is basically just "Bullseye" with some things dumped and    •//
  8. //• one routine added, and a resource file to back the additions.    •//
  9.  
  10. //• The routine was from the old Pascal source - File v1.1, by        •//
  11. //• Cary Clark, 13 May 1985, which I ported to C.  The program        •//
  12. //• was a simple text file opening/saving program.  The about box    •//
  13. //• was the only cool thing about the whole program.                •//
  14.  
  15. //• It's not the cleanest port in the world, but HEY!  It works!    •//
  16. //• I put a 3 tick delay in the scaling loop, to make the text        •//
  17. //• arrival a little more viewable.  The other 180 tick delay was    •//
  18. //• originally 300 ticks.                                            •//
  19.  
  20. //• Enjoy!                                                            •//
  21.  
  22. //• --------------------------------------------------------------- •//
  23.  
  24. //• NewsSplash.c
  25.  
  26. void InitMacintosh (void);
  27. void SetUpMenus (void);
  28. void HandleEvent (void);
  29. void HandleMouseDown (EventRecord *theEvent);
  30. void AdjustMenus (void);
  31. static void enable (MenuHandle menu, short item, short ok);
  32. void HandleMenu (long mSelect);
  33. void main (void);
  34. void NewAbout (void);
  35.  
  36. #define aboutID 128
  37.  
  38. MenuHandle    appleMenu, fileMenu;
  39.  
  40. enum    {
  41.     appleID = 1,
  42.     fileID
  43. };
  44.  
  45. enum    {
  46.     quitItem = 1
  47. };
  48.  
  49. WindowPtr    shell_window;
  50.  
  51. void InitMacintosh(void)
  52. {
  53.     MaxApplZone();
  54.     
  55.     InitGraf(&qd.thePort);
  56.     InitFonts();
  57.     FlushEvents(everyEvent, 0);
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs(0L);
  62.     InitCursor();
  63.  
  64. }
  65.  
  66. void SetUpMenus (void)
  67. {
  68.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  69.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  70.     DrawMenuBar ();
  71.     AppendMenu (appleMenu, "\pAbout item");
  72.     AddResMenu (appleMenu, 'DRVR');
  73.     AppendMenu (fileMenu, "\pQuit/Q");
  74. }
  75.  
  76. void HandleEvent(void)
  77. {
  78.     int            ok;
  79.     EventRecord    theEvent;
  80.  
  81.     HiliteMenu(0);
  82.     SystemTask ();        //• Handle desk accessories.
  83.     
  84.     ok = GetNextEvent (everyEvent, &theEvent);
  85.     if (ok)
  86.         switch (theEvent.what)
  87.         {
  88.             case mouseDown:
  89.                 HandleMouseDown(&theEvent);
  90.             break;
  91.             
  92.             case keyDown: 
  93.             case autoKey:
  94.             if ((theEvent.modifiers & cmdKey) != 0)
  95.             {
  96.                    AdjustMenus();
  97.                 HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  98.             }
  99.             break;
  100.             
  101.             case updateEvt:
  102.                 BeginUpdate(shell_window);
  103.                 EndUpdate(shell_window);
  104.             break;
  105.             
  106.             case activateEvt:
  107.                 InvalRect(&shell_window->portRect);
  108.             break;
  109.         }
  110. }
  111.  
  112. void HandleMouseDown (EventRecord    *theEvent)
  113. {
  114.     WindowPtr    theWindow;
  115.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  116.     
  117.     switch (windowCode)
  118.     {
  119.         case inSysWindow: 
  120.             SystemClick (theEvent, theWindow);
  121.         break;
  122.         
  123.         case inMenuBar:
  124.               AdjustMenus();        //• Left over - not really needed.
  125.             HandleMenu(MenuSelect(theEvent->where));
  126.         break;
  127.             
  128.         case inContent:
  129.             if (theWindow == shell_window)
  130.             {
  131.                 if (theWindow != FrontWindow())
  132.                     SelectWindow(shell_window);
  133.                 else
  134.                     InvalRect(&shell_window->portRect);
  135.             }
  136.         break;
  137.           
  138.         case inGoAway:
  139.         if (theWindow == shell_window && 
  140.             TrackGoAway(shell_window, theEvent->where))
  141.             HideWindow(shell_window);
  142.         break;
  143.     }
  144. }
  145.  
  146. void AdjustMenus(void)
  147. {
  148.     register WindowPeek wp = (WindowPeek) FrontWindow();
  149.     short kind = wp ? wp->windowKind : 0;
  150.     Boolean DA = kind < 0;
  151.         
  152. }
  153.  
  154.  
  155. static void enable(MenuHandle menu, short item, short ok)
  156. {
  157.     if (ok)
  158.         EnableItem(menu, item);
  159.     else
  160.         DisableItem(menu, item);
  161. }
  162.  
  163. void HandleMenu (long mSelect)
  164. {
  165.     int            menuID = HiWord(mSelect);
  166.     int            menuItem = LoWord(mSelect);
  167.     Str255        name;
  168.     GrafPtr        savePort;
  169.     WindowPeek    frontWindow;
  170.     
  171.     switch (menuID)
  172.     {
  173.         case    appleID:
  174.             if (menuItem == 1)
  175.                 NewAbout ();
  176.             else
  177.                 {
  178.                     GetPort (&savePort);
  179.                     GetItem (appleMenu, menuItem, name);
  180.                     OpenDeskAcc (name);
  181.                     SetPort (savePort);
  182.             }
  183.         break;
  184.     
  185.         case    fileID:
  186.             switch (menuItem)
  187.             {              
  188.                 if (frontWindow->windowKind < 0)
  189.                     CloseDeskAcc(frontWindow->windowKind);
  190.                 else 
  191.                     if ((frontWindow = (WindowPeek) shell_window) != NULL)
  192.                         HideWindow(shell_window);
  193.                   break;
  194.                         
  195.                 case    quitItem:
  196.                     ExitToShell();
  197.                 break;
  198.             }
  199.         break;
  200.     }
  201. }
  202.  
  203.  
  204. void main( void)
  205. {
  206.     InitMacintosh();
  207.     SetUpMenus();
  208.         NewAbout ();
  209.     for (;;)
  210.         HandleEvent();
  211. }
  212.  
  213. void NewAbout(void)
  214. {
  215.     #define mouseKey mDownMask + keyDownMask
  216.     StringHandle str1hdl;
  217.     Str255 str1, str2;
  218.     WindowPtr myWindow;
  219.     short width, height, counter, strWidth, strDepth, factor, remainder, adjust;
  220.     long newCount;
  221.     FontInfo txtInfo;
  222.     Rect tempRect, tRect1;
  223.     BitMap offScreen, tempBits;
  224.     EventRecord    theEvent;
  225.  
  226.     str1hdl = (StringHandle)(GetResource (0L, 0L));
  227.     GetIndString (str1, 128, 1);
  228.     GetIndString (str2, 128, 2);
  229.     HLock ((Handle) str1hdl);
  230.     myWindow = GetNewWindow (128, 0L, (WindowPtr)-1L);
  231.     SetPort (myWindow);
  232.     TextFont (0);
  233.     TextSize (12);
  234.     GetFontInfo (&txtInfo);
  235.     width = myWindow->portRect.right - myWindow->portRect.left;
  236.     height = myWindow->portRect.bottom - myWindow->portRect.top;
  237.     strWidth = StringWidth ((StringPtr) &str1hdl);
  238.     if (( StringWidth (str1) > strWidth ) 
  239.     || ( StringWidth (str2) > strWidth ))
  240.     { 
  241.         strWidth = StringWidth (str1);
  242.         strWidth = StringWidth (str2);
  243.     }
  244.     strDepth = txtInfo.ascent * 2 + 
  245.                 txtInfo.descent * 2 + 
  246.                 txtInfo.leading + 1;
  247.                 
  248.     //• rowbytes needs to be even.
  249.     offScreen.rowBytes = (strWidth + 15) / 16 * 2;
  250.     SetRect (&offScreen.bounds, 0,0,strWidth,strDepth);
  251.     offScreen.baseAddr = NewPtrClear (offScreen.rowBytes * strDepth);
  252.     
  253.     tempBits = myWindow->portBits;
  254.     SetPortBits (&offScreen);
  255.     MoveTo ((strWidth - StringWidth (str1)) / 2, strDepth - txtInfo.ascent - 6);
  256.     DrawString (str1);
  257.     MoveTo ((strWidth - StringWidth (str2)) / 2, strDepth - txtInfo.descent);
  258.     DrawString (str2);
  259.     HUnlock ((Handle) str1hdl);
  260.     
  261.     SetPortBits (&tempBits);
  262.     factor = strWidth / strDepth;
  263.     remainder = strWidth % strDepth;
  264.     SetRect (&tRect1, (width - remainder) / 2 - factor, height / 2 - 1,
  265.     (width + remainder) / 2 + factor, height / 2 + 1);
  266.     counter = 1;
  267.     do
  268.     {
  269.         SystemTask ();
  270.         CopyBits (&offScreen, &myWindow->portBits, &offScreen.bounds, &tRect1, srcCopy, 0L);
  271.         InsetRect (&tRect1, - factor, -1);
  272.         counter = counter + 2;
  273.         Delay (3L, &newCount);
  274.     } while (counter != strDepth +1) ;
  275.  
  276.     Delay (180L, &newCount);    //• Wait 5 seconds.
  277.  
  278.     tempRect = offScreen.bounds;
  279.     OffsetRect (&tempRect, (width - strWidth) / 2, (height - strDepth) / 2);
  280.     tRect1 = offScreen.bounds;
  281.  
  282.     while (! EventAvail(mouseKey, &theEvent) &&
  283.           (tRect1.right - factor * 2 > tRect1.left) )
  284.     {
  285.         SystemTask (); //• the clock still ticks!.
  286.         factor = tRect1.right / strDepth;
  287.         if ( tempRect.left > 0 ) 
  288.             InsetRect (&tempRect, - factor, -2);
  289.         else 
  290.             if ( tempRect.top > 0 )
  291.             {
  292.                  InsetRect (&tRect1, factor, 0);
  293.                  InsetRect (&tempRect, 0, -2);
  294.             } 
  295.             else 
  296.                 InsetRect (&tRect1, factor, 2);
  297.                 CopyBits (&offScreen, &myWindow->portBits, &tRect1, &tempRect, srcCopy, 0L);
  298.     }
  299.     DisposPtr(offScreen.baseAddr);
  300.     DisposeWindow (myWindow);
  301. }
  302.